home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / asm_n_z.zip / RECEIVE.ASM < prev    next >
Assembly Source File  |  1986-07-14  |  17KB  |  393 lines

  1.         Page    55,132
  2.         Title   SEND - Send a file using XMODEM protocol
  3.  
  4. SPECIAL Equ     0                       ;If true, include data size in block
  5.  
  6.         If      SPECIAL
  7. BLOCK   Struc
  8. B_SOH   Db      001H                    ;Start of header
  9. B_BLK1  Db      000H                    ;Block number
  10. B_BLK2  Db      0FFH                    ;(Block number)'
  11. B_CNT   Db      ?                       ;Count of bytes used in the data area
  12. B_DATA  Db      128 Dup (?)             ;The data area
  13. B_CHKSUM Db     ?                       ;Checksum of the data
  14. BLOCK   Ends
  15.         Else
  16. BLOCK   Struc
  17. B_SOH   Db      001H                    ;Start of header
  18. B_BLK1  Db      000H                    ;Block number
  19. B_BLK2  Db      0FFH                    ;(Block number)'
  20. B_DATA  Db      128 Dup (?)             ;The data area
  21. B_CHKSUM Db     ?                       ;Checksum of the data
  22. BLOCK   Ends
  23.         Endif
  24.  
  25. ERROR   Macro   N
  26.         Mov     AH,09H                  ;Display the error message
  27.         Lea     DX,ERRMSG&N             ;
  28.         Int     021H                    ;
  29.         Mov     AX,04C10H+N             ;And leave
  30.         Int     021H                    ;
  31.         Endm
  32.  
  33. CODE    Segment Public  'CODE'
  34. CODE    Ends
  35.  
  36. DATA    Segment Public  'DATA'
  37.  
  38. BAUD_TABLE Label Word
  39.         Dw      9600                    ;
  40.         Dw      4800                    ;
  41.         Dw      2400                    ;
  42.         Dw      1200                    ;
  43.         Dw      600                     ;
  44.         Dw      300                     ;
  45.         Dw      150                     ;
  46.         Dw      110                     ;
  47.  
  48. FILENAME Db     64 Dup (0)              ;Name of file to send
  49.  
  50. BUFFER  BLOCK   <>                      ;The block buffer
  51.  
  52. ERRMSG0 Db      '?No output file specified',13,10,'$'
  53. ERRMSG1 Db      '?Unable to create output file',13,10,'$'
  54. ERRMSG2 Db      '?Invalid baud rate specified',13,10,'$'
  55. ERRMSG3 Db      '?Transmitter time-out occurred',13,10,'$'
  56. ERRMSG4 Db      '?Error while writing the output file',13,10,'$'
  57. ERRMSG5 Db      '^C',13,10,'%Reception aborted via control-C',13,10,'$'
  58.  
  59. TXTMSG1 Db      'Waiting for transmitter...',13,10,'$'
  60. TXTMSG2 Db      'Block number '
  61. TXTNUM2 Db      '0    ',13,'$'
  62. TXTMSG3 Db      10,'Reception complete',13,10,'$'
  63. TXTMSG4 Db      'No retries needed',13,10,'$'
  64. TXTMSG5 Db      'Retry count was '
  65. TXTNUM5 Db      '0    ',13,10,'$'
  66.  
  67. MODE    Db      11101011B               ;9600 baud, odd parity, 1 stop bit,
  68.                                         ;8 bit character
  69.  
  70. BLOCK_COUNT Dw  0                       ;Total block count
  71. RETRY_COUNT Dw  0                       ;Retry count
  72.  
  73. BLK_NUM Db      001H                    ;8 bit block number expected.
  74.  
  75. DATA    Ends
  76.  
  77. STACK   Segment Stack   'STACK'
  78.  
  79.         Dw      128 Dup (?)             ;Program stack
  80.  
  81. STACK   Ends
  82.  
  83. CODE    Segment Public  'CODE'
  84.  
  85.         Assume  CS:CODE,DS:Nothing,ES:Nothing,SS:STACK
  86.  
  87. MAIN    Proc    Near
  88.         Sti                             ;Enable interrupts
  89.         Mov     AX,DATA                 ;Set up ES
  90.         Mov     ES,AX                   ;
  91.  
  92.         Assume  ES:DATA
  93.  
  94.         Cld                             ;Make sure the direction flag is clear
  95.         Mov     SI,080H                 ;Use SI to get the file name
  96.         Lodsb                           ;Get the length
  97.         Or      AL,AL                   ;Is there anything??
  98.         Jnz     MAIN01                  ;Yes. Proceed
  99. MAIN00: Mov     AX,ES                   ;Set up DS
  100.         Mov     DS,AX                   ;
  101.  
  102.         Assume  DS:DATA
  103.  
  104.         ERROR   0                       ;Nope. Die!
  105.  
  106.         Assume  DS:Nothing
  107.  
  108. MAIN01: Cbw                             ;Make it a word count
  109.         Mov     CX,AX                   ;Copy to CX
  110.  
  111. MAIN05: Jcxz    MAIN00                  ;If nothing left, error!
  112.         Dec     CX                      ;Decrement the character count
  113.         Lodsb                           ;And get the character
  114.         Cmp     AL,020H                 ;Space or less??
  115.         Ja      MAIN10                  ;Nope. Start the file name
  116.         Jmp Short MAIN05                ;Try again...
  117.  
  118. MAIN10: Lea     DI,FILENAME             ;Point DI to the file name buffer
  119. MAIN11: Stosb                           ;Store the character
  120.         Jcxz    MAIN20                  ;Got the file name. Open the file
  121.         Dec     CX                      ;Decrement the character count
  122.         Lodsb                           ;Get the next character
  123.         Cmp     AL,020H                 ;Is it a displayable character
  124.         Ja      MAIN11                  ;Yes. Put it in the file name
  125.  
  126. MAIN13: Jcxz    MAIN20                  ;No more characters?
  127.         Dec     CX                      ;Decrement the count
  128.         Lodsb                           ;Get another character
  129.         Cmp     AL,020H                 ;Space or less??
  130.         Ja      MAIN15                  ;Yes. See if it's a number
  131.         Jmp Short MAIN13                ;Keep looking
  132.  
  133. MAIN15: Xor     DX,DX                   ;Clear DX
  134. MAIN16: Cmp     AL,'0'                  ;Is this a digit??
  135.         Jae     MAIN18                  ;Yes.
  136. MAIN17: ERROR   2                       ;Not a valid baud rate
  137.  
  138. MAIN18: Cmp     AL,'9'                  ;Still a digit??
  139.         Ja      MAIN17                  ;Nope.
  140.         Sub     AL,'0'                  ;Convert to binary
  141.         Cbw                             ;Convert to a word
  142.         Shl     DX,1                    ;Double the previous number
  143.         Mov     BX,DX                   ;Save in BX for a bit
  144.         Shl     DX,1                    ;
  145.         Shl     DX,1                    ;
  146.         Add     DX,BX                   ;Now DX = (Old DX)*10
  147.         Add     DX,AX                   ;Add in the last digit
  148.         Jcxz    MAIN19                  ;All done.
  149.         Dec     CX                      ;Decrement the character count
  150.         Lodsb                           ;And get a character
  151.         Cmp     AL,020H                 ;Space or less?
  152.         Ja      MAIN16                  ;
  153.  
  154. MAIN19: Mov     AX,DX                   ;Put the baud rate in AX
  155.         Lea     DI,BAUD_TABLE           ;Point to the baud rate table
  156.         Mov     CX,8                    ;Eight entries in the table
  157.         Repne Scasw                     ;Look for this baud rate
  158.         Jne     MAIN17                  ;No good! Die!
  159.         Mov     AL,CL                   ;Get the index
  160.         Mov     CL,5                    ;And position it
  161.         Shl     AL,CL                   ;
  162.         Or      AL,00001011B            ;Odd parity, 1 stop bit, 8 bit character
  163.         Mov     MODE,AL                 ;And save it
  164.  
  165. MAIN20: Mov     AX,ES                   ;Set up DS
  166.         Mov     DS,AX                   ;
  167.  
  168.         Assume  DS:DATA
  169.  
  170.         Mov     AH,03CH                 ;Create the file specified
  171.         Xor     CX,CX                   ;With no attributes
  172.         Lea     DX,FILENAME             ;
  173.         Int     021H                    ;
  174.         Jnc     MAIN25                  ;Proceed!
  175.         ERROR   1                       ;Couldn't create the file
  176.  
  177. MAIN25: Mov     BX,AX                   ;Put the handle in BX
  178.  
  179.         Xor     AH,AH                   ;Initialize the RS232 channel
  180.         Mov     AL,MODE                 ;
  181.         Xor     DX,DX                   ;On channel zero
  182.         Int     014H                    ;
  183.  
  184.         Mov     AH,09H                  ;Display the "Waiting..." message
  185.         Lea     DX,TXTMSG1              ;
  186.         Int     021H                    ;
  187.  
  188.         Mov     CX,64                   ;This shall be our original timeout
  189. MAIN30: Mov     AH,06H                  ;See if a character is waiting
  190.         Mov     DL,0FFH                 ;
  191.         Int     021H                    ;
  192.         Jz      MAIN32                  ;Nope.
  193.         Cmp     AL,3                    ;Is it a control-C??
  194.         Jne     MAIN32                  ;
  195.  
  196.         Mov     AH,03EH                 ;Close the input file
  197.         Int